package com.hefu.virtuallife.ui.foods;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dz.utlis.ScreenUtils;
import com.hefu.virtuallife.R;
import com.hefu.virtuallife.baseui.BaseActivity;
import com.hefu.virtuallife.ui.foods.adapter.IndexMenueAdapter;
import com.hefu.virtuallife.utils.PopWindHelper;
import com.hefu.virtuallife.utils.Tools;
import com.hefu.virtuallife.widget.ToolGridView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import dz.solc.viewtool.RoundIconView;

/**
 * creat_user: zhengzaihong
 * Email:1096877329@qq.com
 * describe 我的菜单
 **/
public class MyCustMenueActivity extends BaseActivity implements PopWindHelper.PopclickListener {

    @BindView(R.id.iv_back)
    ImageButton ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.banner)
    Banner banner;

    @BindView(R.id.gv_menuetype)
    ToolGridView gvMenuetype;
    @BindView(R.id.et_edit_time)
    TextView etEditTime;
    @BindView(R.id.et_edit_address)
    TextView etEditAddress;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tv_who_comment)
    TextView tvWhoComment;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_comment_content)
    TextView tvCommentContent;
    @BindView(R.id.ll_times)
    LinearLayout llTimes;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;
    private IndexMenueAdapter indexMenueAdapter;

    private PopWindHelper popwindhelper;

    private String[] times = new String[]{
            "8:29~8:38", "8:39~8:48", "8:49~8:58",
            "9:19~9:28", "9:29~9:38"
    };
    private String[] address = new String[]{
            "西安南里17号", "顺江小区23号"
    };
    private int typecheck = 0;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_cust_menue_foods;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.VISIBLE);
        tvTitle.setText("自定义菜单");
        tvRight.setText("保存");
        ivBack.setOnClickListener(v -> finish());
    }


    @Override
    protected void initData() {
        super.initData();

        List<Integer> listS = new ArrayList<>();
        listS.add(R.mipmap.banner1);
        listS.add(R.mipmap.banner2);
        listS.add(R.mipmap.banner3);
        banner.setImages(listS);
        banner.setImageLoader(new GlideImageLoader());
        banner.start();
        banner.setOnBannerListener(position -> {
        });

        indexMenueAdapter = new IndexMenueAdapter(mContext);
        gvMenuetype.setAdapter(indexMenueAdapter);

        llTimes.setOnClickListener(view -> {
            typecheck = 0;
            showpop(llTimes, times);
        });
        llAddress.setOnClickListener(view -> {
            typecheck = 1;
            showpop(llAddress, address);
        });
    }

    @Override
    public void callback(int position) {
        if (typecheck == 0) {
            etEditTime.setText(times[position]);
        } else {
            etEditAddress.setText(address[position]);
        }
    }

    public void showpop(LinearLayout ll, Object[] list) {
        if(null!=popwindhelper){
            popwindhelper.closepop();
        }
        popwindhelper = new PopWindHelper(this)
                .initPopAt(R.layout.poplistview, new int[]{(int) ScreenUtils.dip2px(mContext,200f),0})
                .bindClick(this)
                .setAdapterCallBackViewHolder(new PopWindHelper.AdapterCallBackViewHolder() {
                    @Override
                    public void callbackholder(PopWindHelper.TypeAdapter.ViewHolder viewHolder, int position) {
                        ((TextView) (viewHolder.view.findViewById(R.id.time_index)))
                                .setText((list[position].toString()));
                    }
                });
        popwindhelper.bindAdapter(popwindhelper.parentView().findViewById(R.id.lv_datalistview), Arrays.asList(list), R.layout.type_item);
        popwindhelper.show(ll);
    }


    public static class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object banner, ImageView imageView) {
//            Glide.with(context).load(banner.toString()).into(imageView);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundResource(Integer.parseInt(banner.toString()));
        }

        @Override
        public ImageView createImageView(Context context) {
            ImageView imageView = new RoundIconView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            return imageView;
        }
    }
}



